package com.erp.activity

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.widget.AdapterView
import com.erp.activityIKP.ISeekP
import com.erp.activityKP.SeekKP
import com.erp.activityKView.SeekKView
import com.erp.myAdapter.SeekAdapter
import com.erp.bean.BypassBean
import com.erp.R
import com.erp.util.Myutil
import com.erp.view.MyAlerDialog
import kotlinx.android.synthetic.main.activity_seek.*
import kotlinx.android.synthetic.main.header_knoya.view.*

/**
 * Created by Administrator on 2016/11/9/009.
 * 作者：刘洋
 */
class SeekActivity : BaseActivity(),SeekKView, View.OnClickListener, AdapterView.OnItemClickListener {
    override fun gainDataOk() {
    }

    lateinit var adapter: SeekAdapter  //数据适配器
    lateinit var KP : SeekKP
    lateinit var mlist : MutableList<BypassBean> //全部数据
    lateinit var slist : MutableList<BypassBean> //搜索出来的数据
    var select =0
    private val a = "SeekActivity"

    companion object {
        val SEEK_XIAOSHOUYUAN = 1//销售员
        val SEEK_SHEJISHI = 2    //设计师
        val SEEK_KEHUMING = 3//客户名
        val SEEK_TEL = 4//手机号
        val SEEK_ORDER = 5//生产单号
        val SEEK_HETONG = 6//合同号
        val SEEK_ZMD = 7//专卖店
    }

    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_seek
    }


    override fun initCreate() {
        select =intent.getIntExtra("select",0)
        EtSeek.setText(intent.getStringExtra("data"))
        mlist = ArrayList()
        slist = ArrayList()
        adapter=SeekAdapter(slist,this@SeekActivity)
        LvSeek.adapter=adapter
        KP = ISeekP(this, this, mlist, select)
        KP.getData()
        if(select!= SEEK_XIAOSHOUYUAN &&select!= SEEK_SHEJISHI){
            Header.tv_header_right.visibility=View.VISIBLE
            Header.tv_header_right.text="清除"
            Header.tv_header_right.setOnClickListener(this)
        }
        val title =intent.getStringExtra("title")
        Header.tv_header_title.text=title
        Header.iv_header_left.visibility=View.VISIBLE
        Header.iv_header_left.setOnClickListener(this)
        BtSeek.setOnClickListener(this)
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener { KP.shangla() }
        Pull.setOnHeaderRefreshListener { KP.xiala() }
        EtSeek.addTextChangedListener(watcher)
        LvSeek.onItemClickListener = this
    }
    private fun quitdialog() {
        val myAlerDialog = MyAlerDialog(this)
        myAlerDialog.start()
        myAlerDialog.setdata("需要清空搜索数据吗")
        myAlerDialog.setleft("确定") {
            KP.cleansp()
            myAlerDialog.dismiss()
        }
        myAlerDialog.setright("取消") { myAlerDialog.dismiss() }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == 4) {
            finish()
            overridePendingTransition(0, R.anim.end_activity1)
        }
        return super.onKeyDown(keyCode, event)
    }
    //seek输入框文字变化监听
    private val watcher = object : TextWatcher {
        override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) = Unit

        override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            slist.clear()
            mlist.indices
                    .filter { mlist[it].name.indexOf(charSequence.toString()) != -1 }
                    .forEach { slist.add(mlist[it]) }
            adapter.notifyDataSetChanged()
        }

        override fun afterTextChanged(editable: Editable) = Unit
    }
    override fun onClick(v: View?) {
        when(v){
            Header.iv_header_left -> {
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
            Header.tv_header_right -> quitdialog()
            BtSeek -> {
                when(select){
                    SEEK_XIAOSHOUYUAN, SEEK_SHEJISHI -> {
                        var b = true
                        for(bean in mlist){
                            if(bean.name==EtSeek.text.toString()){
                                val resultIntent = Intent()
                                resultIntent.putExtra("name", bean.name)
                                resultIntent.putExtra("uid", bean.uid)
                                setResult(Activity.RESULT_OK, resultIntent)
                                finish()
                                overridePendingTransition(0, R.anim.end_activity1)
                                b=false
                                break
                            }
                        }
                      if(b){
                          if(EtSeek.text.toString()==""){
                              val resultIntent = Intent()
                              resultIntent.putExtra("name", "")
                              resultIntent.putExtra("uid", "")
                              setResult(Activity.RESULT_OK, resultIntent)
                              finish()
                              overridePendingTransition(0, R.anim.end_activity1)
                              b=false
                          }else {
                              EtSeek.setText("")
                              when (select) {
                                  SEEK_XIAOSHOUYUAN -> Myutil.MyTost("找不到这个销售员")
                                  SEEK_SHEJISHI -> Myutil.MyTost("找不到这个设计师")
                              }
                          }
                      }
                    }
                    SEEK_KEHUMING, SEEK_ORDER, SEEK_HETONG, SEEK_TEL, SEEK_ZMD -> {
                        KP.addsp(EtSeek.text.toString())
                        val resultIntent = Intent()
                        resultIntent.putExtra("name", EtSeek.text.toString() + "")
                        setResult(Activity.RESULT_OK, resultIntent)
                        finish()
                        overridePendingTransition(0, R.anim.end_activity1)
                    }
                    0 -> {
                        val resultIntent = Intent()
                        resultIntent.putExtra("name", EtSeek.text.toString() + "")
                        setResult(Activity.RESULT_OK, resultIntent)
                        finish()
                        overridePendingTransition(0, R.anim.end_activity1)
                    }
                }
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when(parent){
            LvSeek -> {
                when(select){
                    SEEK_XIAOSHOUYUAN, SEEK_SHEJISHI -> {
                        val resultIntent = Intent()
                        resultIntent.putExtra("name", slist[position].name)
                        resultIntent.putExtra("uid", slist[position].uid)
                        setResult(Activity.RESULT_OK, resultIntent)
                        finish()
                        overridePendingTransition(0, R.anim.end_activity1)
                    }
                    else -> {
                        EtSeek.setText(slist[position].name)
                    }
                }
            }
        }
    }

    override fun xialaOk() {
        Pull.onHeaderRefreshComplete()
        slist.clear()
        mlist.indices
                .filter { mlist[it].name.indexOf(EtSeek.text.toString()) != -1 }
                .forEach { slist.add(mlist[it]) }
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
       Pull.onHeaderRefreshComplete()
    }

    override fun getDataOk() {
        Pull.onFooterRefreshComplete()
        slist.clear()
        mlist.indices
                .filter { mlist[it].name.indexOf(EtSeek.text.toString()) != -1 }
                .forEach { slist.add(mlist[it]) }
        adapter.notifyDataSetChanged()
    }

    override fun shanglaOk() {
        Pull.onFooterRefreshComplete()
        slist.clear()
        mlist.indices
                .filter { mlist[it].name.indexOf(EtSeek.text.toString()) != -1 }
                .forEach { slist.add(mlist[it]) }
        adapter.notifyDataSetChanged()
    }

    override fun getDataNo() {
        Myutil.MyTost("获取列表失败，下拉可刷新")
    }

    override fun shanglaNo() {
        Myutil.MyTost("获取列表失败，下拉可刷新")
    }

}
